package org.wangblue.common;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;

/**
 * @Description: 公共的链接
 * @Author: WangBlue
 * @Create: 2024-10-22 15:11
 * @Version: 1.0
 **/

public class Client {
    private final static String HOST="localhost";
    private final static int    PORT=9200;
    private final static String PROTOCOL="http";

    private static RestHighLevelClient client = null;

    public static RestHighLevelClient getClient() {
        if (client == null) {
            synchronized (Client.class) {
                if (client == null) {
                    client = new RestHighLevelClient(
                            RestClient.builder(new HttpHost(HOST,PORT,PROTOCOL))
                    );
                }
            }
        }
        return client;
    }

    /**
     * 关闭客户端连接
     */
    public static void close() {
        if (client != null) {
            try {
                client.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

}

